/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package lucava.widget.uspeed;

import java.awt.Color;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JTextPane;
import javax.swing.text.AttributeSet;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
import javax.swing.text.StyledDocument;
import lucava.importer.uspeed.matcher.SyntaxPattern;

/**
 *
 * @author glenn
 */
public class UspeedHighlighter {

    private HashMap<Pattern, AttributeSet> highlightMap;
    private AttributeSet defaultAset;

    public UspeedHighlighter() {
        StyleContext sc = StyleContext.getDefaultStyleContext();
        highlightMap = new HashMap<Pattern, AttributeSet>();

        //Style par défault
        defaultAset = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, Color.BLACK);
        defaultAset = sc.addAttribute(defaultAset, StyleConstants.Bold, false);
        defaultAset = sc.addAttribute(defaultAset, StyleConstants.Italic, false);

        //Style de classe
        Pattern classPattern = Pattern.compile(SyntaxPattern.className);
        AttributeSet classAset = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, Color.BLUE);
        classAset = sc.addAttribute(classAset, StyleConstants.Bold, true);
        highlightMap.put(classPattern, classAset);

        //Style des associations
        Pattern assocPattern = Pattern.compile(SyntaxPattern.assocType);
        AttributeSet assocAset = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, Color.RED);
        highlightMap.put(assocPattern, assocAset);

        //Style des commentaires
        Pattern commentPattern = Pattern.compile(SyntaxPattern.comment);
        AttributeSet commentAset = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, Color.GRAY);
        commentAset = sc.addAttribute(commentAset, StyleConstants.Italic, true);
        highlightMap.put(commentPattern, commentAset);

        //Style de la multiplicité
        Pattern multiPattern = Pattern.compile(SyntaxPattern.multiplicity);
        AttributeSet multiAset = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, Color.GREEN);
        highlightMap.put(multiPattern, multiAset);

        //Style du textes attaché
        Pattern textPattern = Pattern.compile(SyntaxPattern.attachedText);
        AttributeSet textAset = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, Color.GRAY);
        textAset = sc.addAttribute(textAset, StyleConstants.Bold, true);
        highlightMap.put(textPattern, textAset);

        //Style des liens d'héritage
        Pattern inhPattern = Pattern.compile(SyntaxPattern.inheritanceType);
        AttributeSet inhAset = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, Color.RED);
        inhAset = sc.addAttribute(inhAset, StyleConstants.Bold, true);
        highlightMap.put(inhPattern, inhAset);

        //style des type de fleche
        Pattern arrowPattern = Pattern.compile(SyntaxPattern.arrowType);
        AttributeSet arrowAset = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, Color.BLACK);
        arrowAset = sc.addAttribute(arrowAset, StyleConstants.Bold, true);
        highlightMap.put(arrowPattern, arrowAset);

        //Style des modifiers
        Pattern modifPattern = Pattern.compile(SyntaxPattern.modifier);
        AttributeSet modifAset = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, Color.RED);
        modifAset = sc.addAttribute(modifAset, StyleConstants.Bold, true);
        highlightMap.put(modifPattern, modifAset);

        //style des stéréotypes
        Pattern stereoPattern = Pattern.compile(SyntaxPattern.stereotype);
        AttributeSet stereoAset = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, Color.RED);
        highlightMap.put(stereoPattern, stereoAset);



    }

    /**
     * Coloration syntaxique d'un mot dans un Document
     *
     * @param doc
     * @param word
     * @param start
     */
    public void highlightWord(StyledDocument doc, String word, int start) {

        Iterator<Entry<Pattern, AttributeSet>> itPattern = highlightMap.entrySet().iterator();

        doc.setCharacterAttributes(start, word.length() + 1, defaultAset, false);

        while (itPattern.hasNext()) {

            Entry<Pattern, AttributeSet> entry = itPattern.next();
            Matcher matcher = entry.getKey().matcher(word);
            if (matcher.find()) {
                System.out.println(doc.getStartPosition().getOffset() + matcher.group() + matcher.end() + 1);
                doc.setCharacterAttributes(start + matcher.start() - 1, matcher.end() + 1, entry.getValue(), false);
                break;

            }
        }

    }

}
